﻿@{
    
    Layout = null;
}

<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />

<input type="text" id="id" hidden="hidden" value="@ViewBag.Id" />
<form class="layui-form" action="" style="margin-top:30px" lay-filter="fantian">
    <div class="layui-form-item">
        <label class="layui-form-label">单位编码</label>
        <div class="layui-input-block">
            <input type="text" id="unitMeasureCode" lay-verify="required" placeholder="请输入单位编码" autocomplete="off" class="layui-input" style="width:300px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单位名称</label>
        <div class="layui-input-block">
            <input type="text" id="unitMeasureName" lay-verify="required" placeholder="请输入单位名称" autocomplete="off" class="layui-input" style="width:300px">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否是主单位</label>
        <div class="layui-input-block">
            <input type="radio" id="isPrimaryUnitYes" name="isPrimaryUnit" lay-filter="isPrimaryUnits" value="true" title="是" onchange="inputVal()">
            <input type="radio" id="isPrimaryUnitNo" name="isPrimaryUnit" lay-filter="isPrimaryUnits" value="false" title="否">
        </div>
    </div>
    <div id="mainUnitFieIds" style="display:none;">
        <div class="layui-form-item">
            <label class="layui-form-label">主单位</label>
            <div class="layui-input-block">
                <select id="mainUnit" name="mainUnit" lay-search style="width:300px">
                    <option value="">请选择主单位</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">与主单位换算比例</label>
            <div class="layui-input-block">
                <input type="text" id="conversionScale" name="conversionScale" lay-verify="required" placeholder="请输入换算比例" autocomplete="off" class="layui-input" style="width:300px">
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="radio" id="stateYes" name="state" value="true" title="是" >
            <input type="radio" id="stateNo"  name="state" value="false" title="否">

        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="update">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">取消</button>
        </div>
    </div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script>

    //关闭弹框
    function closeWorkStation() {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
        parent.layer.close(index);//再执行关闭
    }

    layui.use(['form', 'laydate', 'util'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var util = layui.util;

        // 监听是否主单位的选择
        form.on('radio(isPrimaryUnits)', function (data) {
            var value = data.value;// 获取选中的值
            inputVal(value);

        })
        function inputVal(value) {
            if (value == "true") {
                // 如果是主单位，隐藏额外的字段
                $('#mainUnitFieIds').hide();

            }
            else {
                // 如果不是主单位，显示额外的字段
                $('#mainUnitFieIds').show();
                GetDlll()
            }
        }

        // 提交事件
        form.on('submit(update)', function (data) {
            var field = data.field; // 获取表单字段值
            field = {
                id: $('#id').val(),
                unitMeasureCode: $('#unitMeasureCode').val(),
                unitMeasureName: $('#unitMeasureName').val(),
                isPrimaryUnit: $('#isPrimaryUnitYes').prop('checked')?true:false,
                mainUnit: $('#mainUnit').val(),
                conversionScale: $('#conversionScale').val(),
                state: $('#stateYes').prop('checked') ? true : false,
            }
            $.ajax({
                type: 'post',
                url: '/UnitMeasure/UpdateUnitMeasure',
                data: field,
                dataType: 'json',
                success: function (res) {
                    if (res > 0) {
                        layer.msg('修改成功')
                        closeWorkStation();
                        return false;
                    }
                    if (res == -1) {
                        layer.msg('单位编号已存在')
                        return false;
                    }
                    if (res == -2) {
                        layer.msg('单位名称已存在')
                        return false;
                    }
                },

            })
            return false; // 阻止默认 form 跳转
        });

        function GetDlll() {
            $.ajax({
                url: '/UnitMeasure/GetUnitMeasure', // 替换为你的 API 路径  
                type: 'GET',
                dataType: 'json',
                async: false,
                success: function (res) {
                    console.log(res)
                    // 清空现有选项  
                    $('#mainUnit').empty();
                    $('#mainUnit').append('<option value="0">请选择主单位</option>');

                    // 遍历返回的数据  
                    $.each(res, function (index, vendor) {
                        $('#mainUnit').append(
                            '<option value="' + vendor.id + '">' + vendor.unitMeasureName + '</option>'
                        );
                    });
                    // 重新渲染 layui 组件
                    layui.form.render('select');
                },
                error: function (xhr, status, error) {
                    console.error('获取供应商失败:', error);
                }
            });
        }

   
        $(document).ready(function () {
            getUnitMeasureById()

            // 监听下拉框变化  
            $('#mainUnit').change(function () {
                var selectedValue = $(this).val();
                console.log("选择的供应商ID:", selectedValue);
                // 根据选择的主单位进行其他操作  
            });

        });  



        //反填
        function getUnitMeasureById() {
            // 页面加载时获取地址栏中的 id 参数
            var id = @ViewBag.id;
            //根据ID值获取数据
            $.ajax({
                type: 'get',
                url: '/UnitMeasure/GetUnitMeasureById',// 替换成实际的后端接口地址
                data: { id: id },// 发送表单字段数据
                dataType: 'json',
                async: false,
                success: function (res) {
                    console.log(res)
                    $('#id').val(res.id);
                    $("#unitMeasureCode").val(res.unitMeasureCode);
                    $("#unitMeasureName").val(res.unitMeasureName);
                    $("#isPrimaryUnit").val(res.isPrimaryUnit);
                    if (res.isPrimaryUnit) {
                        $('#isPrimaryUnitYes').prop('checked', true);
                    }
                    else {
                        $('#isPrimaryUnitNo').prop('checked', true);
                    }
                    inputVal(res.isPrimaryUnit)
                    setTimeout(function () {
                        $("#mainUnit").val(res.mainUnit);
                        if (res.state) {
                            $("#stateYes").prop("checked", true);
                        }
                        else {
                            $("#stateNo").prop("checked", true);
                        }
                        $("#conversionScale").val(res.conversionScale);

                        // 重新渲染 layui 组件
                        layui.form.render('radio');

                        // 重新渲染 layui 组件
                        layui.form.render('select');// 渲染 select 类型的表单元素
                    },330)

                }
            })
        }

    });

</script>
